package org.wolfengi.handler.util;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

import java.io.*;

/**
 * @Author lf
 * @Date 2018/3/6 14:37
 */
public class HDFSUtils {
    static FileSystem fs = null;
    static Configuration conf = null;

    static {
        conf = new Configuration();
        try {
            fs = FileSystem.get(conf);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("hdfs初始化FileSystem失败！");
        }
    }

    /**
     * 获取 FileSystem 对象
     *
     * @return
     */
    public static FileSystem getFileSystem() {
        return fs;
    }

    /**
     * 关闭 FileSystem
     *
     * @param fs
     * @throws IOException
     */
    public static void close(FileSystem fs) throws IOException {
        if (null != fs) {
            try {
                fs.close();
            } catch (IOException e) {
                System.out.println("关闭FileSystem失败！");
                throw e;
            }
        }
    }

    public static boolean deleteRes(String path) {
        try {
            FileSystem fileSystem = getFileSystem();
            boolean b = fileSystem.delete(new Path(path), true);
            fileSystem.close();
            return b;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean hdfsFileIsExists(String path) {
        FileSystem fileSystem = getFileSystem();
        try {
            boolean b = fileSystem.exists(new Path(path));
            return b;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }


    public static void uploadFile(String localFileName, String url) {
        FileSystem fs = getFileSystem();
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(localFileName));
            FSDataOutputStream fout = fs.create(new Path(url));
            IOUtils.copyBytes(in, fout, 1024, true);
            System.out.println("上传成功！");
            fout.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void uploadFileCharset(String localFileName, String url, String localFileCharset) {
        FileSystem fs = getFileSystem();
        try {
            InputStream in = transformFileCharset(localFileName, localFileCharset);
            FSDataOutputStream fout = fs.create(new Path(url));
            IOUtils.copyBytes(in, fout, 1024, true);
            System.out.println("上传成功！");
            fout.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private static InputStream transformFileCharset(String filePath, String charsetName) throws IOException {
        InputStreamReader is = new InputStreamReader(new FileInputStream(filePath), charsetName);
        BufferedReader br = new BufferedReader(is);
        StringBuffer sbf = new StringBuffer();
        String line;
        while ((line = br.readLine()) != null) {
            sbf.append(line + "\n");
        }
        br.close();
        is.close();
        ByteArrayInputStream bin = new ByteArrayInputStream(sbf.toString().getBytes());
        return bin;
    }
}
